package com.lab.web.controller.biz;

import com.lab.common.annotation.Log;
import com.lab.common.core.controller.BaseController;
import com.lab.common.core.domain.AjaxResult;
import com.lab.common.core.domain.entity.SysRole;
import com.lab.common.core.domain.model.LoginUser;
import com.lab.common.core.page.TableDataInfo;
import com.lab.common.enums.BusinessType;
import com.lab.common.utils.poi.ExcelUtil;
import com.lab.system.domain.Lab;
import com.lab.system.domain.qo.LabQO;
import com.lab.system.service.ILabService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 实验室Controller
 *
 * @author cf
 * @date 2025-02-09
 */
@RestController
@RequestMapping("/biz/lab")
public class LabController extends BaseController {
    @Autowired
    private ILabService labService;

    /**
     * 查询实验室列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Lab lab) {
        startPage();
        LoginUser loginUser = getLoginUser();
        List<Lab> list = labService.selectLabList(lab,loginUser);
        return getDataTable(list);
    }

    /**
     * 预约实验室查询列表
     */
    @GetMapping("/appointmentList")
    public TableDataInfo appointmentList(LabQO labQO) {
        startPage();
        List<Lab> list = labService.selectLabByLabQO(labQO);
        return getDataTable(list);
    }


    /**
     * 生成实验室编号
     */
    @PreAuthorize("@ss.hasPermi('biz:lab:add')")
    @GetMapping("/generateLabNo")
    public AjaxResult generateLabNo() {
        String labNo = labService.generateLabNo();
        return success(labNo);
    }


    /**
     * 导出实验室列表
     */
    @PreAuthorize("@ss.hasPermi('biz:lab:export')")
    @Log(title = "实验室", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Lab lab) {
        LoginUser loginUser = getLoginUser();
        List<Lab> list = labService.selectLabList(lab,loginUser);
        ExcelUtil<Lab> util = new ExcelUtil<Lab>(Lab.class);
        util.exportExcel(response, list, "实验室数据");
    }

    /**
     * 获取实验室详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(labService.selectLabById(id));
    }

    /**
     * 新增实验室
     */
    @PreAuthorize("@ss.hasPermi('biz:lab:add')")
    @Log(title = "实验室", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Lab lab) {
        lab.setCreatedBy(getUserId().toString());
        lab.setUpdateBy(getUserId().toString());
        return toAjax(labService.insertLab(lab));
    }

    /**
     * 修改实验室
     */
    @PreAuthorize("@ss.hasPermi('biz:lab:edit')")
    @Log(title = "实验室", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Lab lab) {
        lab.setUpdateBy(getUserId().toString());
        lab.setUpdatedAt(new Date());
        return toAjax(labService.updateLab(lab));
    }

    /**
     * 删除实验室
     */
    @PreAuthorize("@ss.hasPermi('biz:lab:remove')")
    @Log(title = "实验室", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(labService.deleteLabByIds(ids));
    }
}
